package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.uwm.universitydb.model.Department;
import edu.uwm.universitydb.model.Student;
import edu.uwm.universitydb.service.ServiceFactory;
import edu.uwm.universitydb.service.dept.DepartmentService;
import edu.uwm.universitydb.swing.StudentTableModel;


public class StudentMaint extends JPanel {
	
	public StudentMaint(StudentTableModel dataService) {
		_dataService = dataService;
		this.setLayout(new BorderLayout());
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout());
		titlePanel.setBackground(Color.WHITE);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 16, 0, Color.WHITE));
		JLabel title = new JLabel("Student Maintenance");
		title.setFont(new Font(title.getFont().getName(), title.getFont().getStyle(), 18));
		titlePanel.add(title);

		this.add(titlePanel, BorderLayout.NORTH);
		this.add(this.createAppView(), BorderLayout.CENTER);
		
	}
	
	private JPanel createAppView() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		//Create Edit/Delete buttons panel
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		_editButton = new JButton(StudentMaint.EDITBUTTON);
		_editButton.setIcon(new ImageIcon("res/edit.png"));
		_editButton.setName(StudentMaint.EDITBUTTON);
		
		_deleteButton = new JButton(StudentMaint.DELBUTTON);
		_deleteButton.setIcon(new ImageIcon("res/remove.png"));
		_deleteButton.setName(StudentMaint.DELBUTTON);
		
		_addButton = new JButton(StudentMaint.ADDBUTTON);
		_addButton.setIcon(new ImageIcon("res/add.png"));
		_addButton.setName(StudentMaint.ADDBUTTON);
		
		btnPanel.setBackground(Color.WHITE);
		btnPanel.add(_addButton);
		btnPanel.add(_editButton);
		btnPanel.add(_deleteButton);
		panel.add(btnPanel);

		//Create Table
		_datatable = new JTable(_dataService);
		_datatable.setName("DataTbl");
		_datatable.setAutoCreateRowSorter(true);
		_datatable.setAlignmentX(Component.CENTER_ALIGNMENT);
		_datatable.getColumnModel().getColumn(0).setMaxWidth(110);
		_datatable.getColumnModel().getColumn(0).setMinWidth(110);
		_datatable.getColumnModel().getColumn(2).setMaxWidth(50);
		_datatable.getTableHeader().setReorderingAllowed(false);

		JScrollPane tblPane = new JScrollPane(_datatable);
		panel.add(tblPane);
		
		panel.add(Box.createRigidArea(new Dimension(0,16)));
		return panel;
	}
	
	public JPanel showMaintView(Student student) {
		JPanel panel = new JPanel(new GridLayout(0, 1));
		// SSN
		panel.add(new JLabel("SSN"));
		JTextField ssnFild = new JTextField(student.getSsnNum());
		ssnFild.setName(SSNFIELD);
		ssnFild.setEditable(false);
		panel.add(ssnFild);
		
		//Name
		panel.add(new JLabel("Name"));
		JTextField nameField = new JTextField(student.getName());
		nameField.setName(NAMEFIELD);
		panel.add(nameField);
		
		//Age
		panel.add(new JLabel("Age"));
		JTextField ageField = new JTextField(student.getStrAge());
		ageField.setName(AGEFIELD);
		panel.add(ageField);
		
		//Degree
		panel.add(new JLabel("Degree"));
		JTextField degreeField = new JTextField(student.getProgram());
		degreeField.setName(DEGREEFIELD);
		panel.add(degreeField);
		
		//Department
		panel.add(new JLabel("Department"));
		DepartmentService depts = ServiceFactory.getInstance().getDepartmentService();
		List<Department> deptList = depts.getDepartments();
		JComboBox<Department> deptField = new JComboBox<Department>(deptList.toArray(new Department[deptList.size()]));
		deptField.setSelectedItem(student.getDepartment());
		deptField.setName(DEPTFIELD);
		panel.add(deptField);
		
		return panel;
	}
	
	public JPanel showNewView() {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		// SSN
		panel.add(new JLabel("SSN"));
		JTextField ssn = new JTextField();
		ssn.setName(SSNFIELD);
		panel.add(ssn);
		
		//Name
		panel.add(new JLabel("Name"));
		JTextField name = new JTextField();
		name.setName(NAMEFIELD);
		panel.add(name);
		
		//Age
		panel.add(new JLabel("Age"));
		JTextField age = new JTextField();
		age.setName(AGEFIELD);
		panel.add(age);
		
		//Degree
		panel.add(new JLabel("Degree"));
		JTextField degree = new JTextField();
		degree.setName(DEGREEFIELD);
		panel.add(degree);
		
		//Department
		panel.add(new JLabel("Department"));
		DepartmentService depts = ServiceFactory.getInstance().getDepartmentService();
		List<Department> deptList = depts.getDepartments();
		JComboBox<Department> dept = new JComboBox<Department>(deptList.toArray(new Department[deptList.size()]));
		dept.setName(DEPTFIELD);
		dept.setSelectedIndex(-1);
		panel.add(dept);
		
		return panel;
	}
	
	public void refreshTable() {
	    _dataService.refreshData();
	    _datatable.revalidate();
	    _datatable.repaint();	
	}
	
	public JButton getAddButton() {
		return _addButton;
	}
	
	public JButton getEditButton() {
		return _editButton;
	}
	
	public JButton getDelButton() {
		return _deleteButton;
	}
	
	public JTable getDataTable() {
		return _datatable;
	}
	
	
	private JButton _editButton;
	private JButton _deleteButton;
	private JButton _addButton;
	private JTable _datatable;
	
	private StudentTableModel  _dataService;
	
	public static final String EDITBUTTON = "Edit";
	public static final String DELBUTTON = "Delete";
	public static final String ADDBUTTON = "New";
	
	public static final String SSNFIELD = "SSN";
	public static final String NAMEFIELD = "NAME";
	public static final String AGEFIELD = "AGE";
	public static final String DEGREEFIELD = "DEGREE";
	public static final String DEPTFIELD = "DEPT";


	private static final long serialVersionUID = -8014234004942769650L;

}
